/*
 * GeneratorBean.java		1.00	2008. 01. 21
 *
 * Copyright (c) 2008 David.Kim(KJS). All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of David.Kim(KJS).  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with David.Kim(KJS).
 */
package doit.gen.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.Map;

import doit.framework.util.Config;

/**
 * <pre>
 * system      : 공통
 * menu        : 공통
 * source      : GeneratorBean.java
 * description : 테이블에 매핑 되는 Bean 생성
 * </pre> 
 * @version
 * <pre> 
 * 1.0	2008. 01. 21  leewan  최초 작성
 * 1.1	
 * </pre>
 */

@SuppressWarnings("unchecked")
public class GenerateJspPageList implements BaseJspGenerate{
	public void generate( Map input, String tableName, String[] columns) throws Exception {
	    String packageName = (String) input.get("packageName");
        String basePath = Config.getString("doit.generator.path.base");
        String parentPath = basePath + "/" + packageName;
        String childPath = Config.getString("doit.generator.path.jsp");
        String layoutJspPath = Config.getString("doit.generator.layout.pageList");

        // 폴더 생성
		GeneratorUtil.makeDirectorys( parentPath, childPath);

		// 클래스명 분석
		String className = GeneratorUtil.getClassName( tableName);
		
		// 내용 생성
		StringBuffer sb = new StringBuffer();
		
		// Layout JSP 파일 Read
		sb.append( printJsp(layoutJspPath));

		// 파일 생성
		File file = GeneratorUtil.makeFile( parentPath + "\\" + childPath + "\\" + className + "List." + "jsp");
		// 파일에 쓰기
		RandomAccessFile raf = new RandomAccessFile( file, "rw");
		GeneratorUtil.write( raf, sb.toString());
		
		// 파일 닫기
		raf.close();
		
	}
	
	public String printJsp(String layoutJspPath) throws Exception {
		StringBuffer sb = new StringBuffer();
		File jspFile = new File(layoutJspPath);
		
		if(jspFile.exists()){
			FileInputStream fin = new FileInputStream(jspFile);  
			BufferedReader br = new BufferedReader(new InputStreamReader(fin, "UTF-8"));
			
			String line = "";
			while((line = br.readLine() ) != null ){
				sb.append(line + "\n");
			}
			fin.close();
			br.close();
		}
		sb.append("\n");
		return sb.toString();
	}
}
